/**
 * Author shuqi
 * Rev
 * Date: 2020-01-01 8:36
 * <p>
 * Copyright (C) 2020 Seeyon, Inc. All rights reserved.
 * <p>
 * This software is the proprietary information of Seeyon, Inc.
 * Use is subject to license terms.
 *
 * @company seeyon.com
 * @since V5 V8.0
 * @author shuqi
 */
package com.shuqi.tools.actions;

import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.shuqi.tools.CMPHelper;
import com.shuqi.tools.dto.BuildConfig;
import com.shuqi.tools.ui.ConfigPanel;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.File;

/**
 * <p>配置面板类</p>
 * @date 2020-01-01 8:36
 * @since V5 V8.0
 * @author shuqi
 */
public class CMPHelperConfig implements Configurable {
	private  ConfigPanel configPanel;
	private  Project project;

	public CMPHelperConfig(Project project) {
		this.configPanel = new ConfigPanel();
		this.project = project;
	}

	@Nls(capitalization = Nls.Capitalization.Title)
	@Override
	public String getDisplayName() {
		return "CMP Helper Config";
	}
	@Nullable
	@Override
	public JComponent createComponent() {
		BuildConfig config = CMPHelper.getHelper(project).getConfig();
		// 初始panel
		configPanel.getV5RuntimePath().setText(config.getV5RuntimePath());
		configPanel.getSysPwd().setText(config.getSysPwd());
		configPanel.getM3SourcePath().setText(config.getM3SourcePath());
		configPanel.getM3ToolsPath().setText(config.getM3ToolsPath());
		return configPanel.getRootPanel();
	}

	@Override
	public boolean isModified() {
		BuildConfig config = CMPHelper.getHelper(project).getConfig();
		// 检查配置是否改变
		return !configPanel.getV5RuntimePath().getText().equals(config.getV5RuntimePath())
				|| !configPanel.getSysPwd().getText().equals(config.getSysPwd())
				|| !configPanel.getM3SourcePath().getText().equals(config.getM3SourcePath())
				|| !configPanel.getM3ToolsPath().getText().equals(config.getM3ToolsPath());
	}

	@Override
	public void apply() throws ConfigurationException {
		String v5RuntimePath = configPanel.getV5RuntimePath().getText();
		String m3SourcePath = configPanel.getM3SourcePath().getText();
		String m3ToolsPath = configPanel.getM3ToolsPath().getText();
		try {
			// 校验v5目录
			File v5RuntimeFile = new File(v5RuntimePath);
			if(StringUtils.isNotBlank(v5RuntimePath)){
				if(!v5RuntimeFile.exists() || !v5RuntimeFile.isDirectory()){
					throw new ConfigurationException(v5RuntimePath + ",不是有效的目录");
				}
			}

			// 校验源码目录
			if(StringUtils.isNotBlank(m3SourcePath)) {
				File m3SourceFile = new File(m3SourcePath);
				if (!m3SourceFile.exists() || !m3SourceFile.isDirectory()) {
					throw new ConfigurationException(m3SourcePath + ",不是有效的目录");
				}
				String basePath = project.getBasePath();
				if (!m3SourceFile.getCanonicalPath().replace('\\', '/').startsWith(basePath.replace('\\', '/'))) {
					throw new ConfigurationException(m3SourcePath + ",不是当前项目[" + basePath + "]的子目录");
				}
			}

			// 校验M3目录
			if(StringUtils.isNotBlank(m3ToolsPath)){
				File m3ToolsFile = new File(m3ToolsPath);
				if(!m3ToolsFile.exists() || !m3ToolsFile.isDirectory()){
					throw new ConfigurationException(m3ToolsPath + ",不是有效的目录");
				}
				//包含打包需要的libs/xxx.jar和jdk
				if(!new File(m3ToolsPath,"libs").exists()){
					throw new ConfigurationException(m3ToolsPath + "/libs,不存在");
				}
				if(!new File(m3ToolsPath,"jdk").exists()){
					throw new ConfigurationException(m3ToolsPath + "/jdk,不存在");
				}
			}
		}catch (ConfigurationException e){
			throw e;
		}catch (Exception e){
			throw new ConfigurationException(e.getLocalizedMessage());
		}
		BuildConfig config = new BuildConfig();
		config.setV5RuntimePath(v5RuntimePath);
		config.setM3SourcePath(m3SourcePath);
		config.setSysPwd(configPanel.getSysPwd().getText());
		config.setM3ToolsPath(m3ToolsPath);
		// 使用设置
		CMPHelper.getHelper(project).storeConfig(config);
	}

}
